// import Vuex from 'vuex'
import { login } from '@/api/login.js'

// 读取 localStorage 中保存的用户信息
const user = JSON.parse(sessionStorage.getItem('user')) || {
  userName: '',
  isLogin: false
}

export default ({
  state: user,
  // strict: true,
  // plugins: [userStorage],
  // state: {
  //   user
  // },
  mutations: {
    login (state, { userName }) {
      console.log(userName)
      state.userName = userName
      state.isLogin = true
    },
    logout (state) {
      state.userName = ''
      state.isLogin = false
    }
  },
  actions: {
    // 异步登录
    loginAsync (store, formMess) {
      return new Promise((resolve, reject) => {
        login(formMess)
          .then(res => {
            console.log(res.data)
            if (res.data.code === 200) {
              const userName = formMess.userName
              store.commit('login', {
                userName
              })
              alert('登录成功')
              resolve()
            } else {
              alert('用户名或密码错误')
            }
          }).catch(err => reject(err))
      })
    }
  },
  modules: {
  }
})
